﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Param.aspx.cs" Inherits="Haolaike.Page.System.Param" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>参数设置管理</title>
    <link rel="stylesheet" type="text/css" href="../../css/theme.css" />
    <link rel="stylesheet" type="text/css" href="../../css/default/easyui.css" />
    <link rel="stylesheet" type="text/css" href="../../css/icon.css" />
    <script src="../../js/jquery-1.8.0.min.js" type="text/javascript"></script>
    <script src="../../js/jquery.easyui.min.js" type="text/javascript"></script>
    <script src="../../js/Json.js" type="text/javascript"></script>
    <script src="../../js/Common.js" type="text/javascript"></script>
    
    <style type="text/css">
        .ftitle
        {
            font-size: 14px;
            font-weight: bold;
            padding: 5px 0;
            margin-bottom: 10px;
            border-bottom: 1px solid #ccc;
        }
        .fitem
        {
            margin-bottom: 5px;
        }
        .fitem label
        {
            display: inline-block;
            width: 80px;
        }
        .searchBlock
        {
            padding-left: 20px;
        }
        .searchBlock span
        {
        }
        
        .searchTitle
        {
            padding-right: 2px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    
    <div id="container" style="width: 100%; padding: 5px">
        <div id="toolbar" style="text-align: left; padding-bottom: 10px">
            <a href="javascript:void(0)" class="easyui-linkbutton" onclick="pageJs.search()">
                搜索</a>
            <a href="javascript:void(0)" class="easyui-linkbutton" id="add" onclick="pageJs.add()">
                新增</a> 
            <a href="javascript:void(0)" class="easyui-linkbutton" id="edit" onclick="pageJs.edit()">
                编辑</a> 
            <a href="javascript:void(0)" class="easyui-linkbutton" id="del" onclick="pageJs.del()">
                删除</a>
            <div style="padding-top: 5px">
                <span class="searchTitle">参数类型</span>
                <input style="width:120px;" id="sPARAM_TYPE" />
                <span class="searchTitle">参数代码</span>
                <input style="width: 110px" id="sPARAM_CODE" />
                <span class="searchBlock"> 
                    <span class="searchTitle">参数名称</span>
                    <input style="width: 110px" id="sPARAM_NAME" />
                </span>
            </div>
        </div>
        <div id="divTable" style="text-align: left">
            <div id="divGrid" style="float: left;">
                <table id="dg" title="参数列表" style="height: 350px;" pagination="true"
                    autorowheight="false" rownumbers="true" fitcolumns="false" singleselect="true">
                    <thead>
                        <tr>
                            <th field="PARAM_TYPE" width="80">
                                参数类型
                            </th>
                            <th field="PARAM_CODE" width="80">
                                参数代码
                            </th>
                            <th field="PARAM_NAME" width="150">
                                参数名称
                            </th>
                            <th field="MEMO" width="150">
                                备注说明
                            </th>
                        </tr>
                    </thead>
                </table>
                
                <div id="pnlDlg" style="display: none">
                    <div id="dlg" style="width: 400px; height: 350px; padding: 10px 20px;" title="add"
                        data-options="iconCls:'icon-save',resizable:true,modal:true,closed:true" buttons="#dlg-buttons">
                        <div class="ftitle">
                            参数明细</div>
                        <div class="fitem">
                            <label>参数代码:</label>
                            <input name="PARAM_CODE" id="PARAM_CODE" class="easyui-validatebox" style="ime-mode: disabled;"
                                disabled="disabled" />
                        </div>
                        <div class="fitem">
                            <label>参数名称:</label>
                            <input name="PARAM_NAME" id="PARAM_NAME" class="easyui-validatebox" />
                            <input name="PARAM_TYPE" id="PARAM_TYPE" type="hidden" />
                            <input name="PARAM_KEY" id="PARAM_KEY" type="hidden" />
                        </div>
                        <div class="fitem">
                            <label>备注说明:</label>
                            <input name="MEMO" id="MEMO" />
                        </div>
                    </div>
                    <div id="dlg-buttons">
                        <a href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-ok" onclick="pageJs.doSave()">
                            保存</a> 
                        <a href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-cancel"
                            onclick="javascript:$('#dlg').dialog('close')">取消</a>
                    </div>
                </div>
            </div>
        </div>
    </div>
    </form>
    
    <script type="text/javascript">
        var pageJs = {
            url: "",
            resize: function(h, w) {
                $('#container').height(h).width(w - 10);
                var contentH = h - $("#toolbar").outerHeight() - 10;
                if (contentH < 120) contentH = 120;
                $("#dg").datagrid("resize", { height: contentH });
                var contentW = $("#divTable").width() - 10;
                if (contentW < 150) contentW = 150;
                $("#dg").datagrid("resize", { width: contentW });
            },
            validate: function() {
                var ParamID = $.trim($("#PARAM_CODE").val());
                var ParamName = $.trim($("#PARAM_NAME").val());
                if (ParamID == "" || ParamID.length > 20)
                    return "参数代码为必须项，最长20字符！";
                if (ParamName == "" || ParamName.length > 50)
                    return "参数名称为必须项，最长50字符！";

                return "";
            },
            cancel: function() {
                var ParamKey = $("#dg").datagrid("getSelected").PARAM_KEY;
            },
            doSave: function() {
                var valMsg = this.validate();
                if (valMsg != "") {
                    BasicJs.showMessage(valMsg);
                    return;
                }
                var parm = { PARAM_CODE: $.trim($("#PARAM_CODE").val()), PARAM_NAME: $.trim($("#PARAM_NAME").val()),
                    MEMO: $.trim($("#MEMO").val()), PARAM_TYPE: $.trim($("#PARAM_TYPE").val())
                };
                var action = "add";
                if ($("#PARAM_KEY").val() != "0") action = "edit";
                $.post('../../ashx/System/hParam.ashx?action=' + action,
                    { data: JSON.stringify(parm) },
                    function(result) {
                        if (result.Success) {
                            if ($("#PARAM_KEY").val() != "0") {
                                //edit
                                var callback = function() { $('#dlg').dialog('close'); };
                                BasicJs.showMessage("保存成功！", true, callback);
                            }
                            else {//add
                                BasicJs.showMessage("保存成功！", true);
                                pageJs.initAdd();
                                setTimeout('$("#PARAM_NAME").focus()', 100);
                            }
                            $('#dg').datagrid('reload');
                        }
                        else {
                            //error
                            if (result.Message == "此参数已被删除，是否还原？") {
                                $.messager.confirm('还原参数', result.Message, function(r) {
                                    if (r) {
                                        $.post(pageJs.url + "?action=revert",
                                            { data: JSON.stringify(parm) },//{ PARAM_CODE: $.trim($("#PARAM_CODE").val()), PARAM_TYPE: $("#PARAM_TYPE").val() },
                                            function(result) {
                                                if (result.Success) {
                                                    $('#dg').datagrid('reload');    // reload the user data
                                                    pageJs.initAdd();
                                                    BasicJs.showMessage("还原成功！", true);
                                                } else {
                                                    if (result.Message) BasicJs.showMessage(result.Message);
                                                }
                                            }, 'json');
                                    }
                                });
                            }
                            else BasicJs.showMessage(result.Message);
                        }
                        //close dialog

                    }, "json"
             );
            },
            initAdd: function() {
                $("#PARAM_KEY").val("0");
                $("#PARAM_CODE").val("").removeAttr("disabled");
                $("#PARAM_NAME").val("").removeAttr("disabled");
                $("#MEMO").val(""); 
                $("#PARAM_TYPE").val($("#sPARAM_TYPE").combobox("getValue"));
            },
            add: function() {
                this.initAdd();
                var pos = BasicJs.getCenter("#dlg");
                $("#pnlDlg").show();
                var position = BasicJs.autoDialogSize(350, 400);
                $('#dlg').dialog(pos).dialog({
                    left: position.Left,
                    top: position.Top,
                    height: position.Height,
                    width: position.Width,
                    "onOpen": function() {
                        setTimeout(' $("#PARAM_CODE").focus();', 100);
                    }
                }).dialog('open').dialog('setTitle', '新增参数');
            },
            edit: function() {
                var row = $('#dg').datagrid('getSelected');
                if (row) {
                    $("#PARAM_KEY").val("1");
                    $("#PARAM_CODE").val(row.PARAM_CODE).attr("disabled", "disabled");
                    $("#PARAM_NAME").val(row.PARAM_NAME).attr("disabled", "disabled");
                    $("#MEMO").val(row.MEMO);
                    $("#PARAM_TYPE").val(row.PARAM_TYPE);
                    var pos = BasicJs.getCenter("#dlg");
                    $("#pnlDlg").show();
                    var position = BasicJs.autoDialogSize(350, 400);
                    $('#dlg').dialog(pos).dialog({
                        left: position.Left,
                        top: position.Top,
                        height: position.Height,
                        width: position.Width,
                        "onOpen": function() {
                            setTimeout(' $("#PARAM_NAME").focus();', 100);
                        }
                    }).dialog('open').dialog('setTitle', '编辑参数');
                }
                else { BasicJs.showMessage("未选中记录！"); }
            },
            del: function() {
                var row = $('#dg').datagrid('getSelected');
                if (row) {
                    $.messager.confirm('删除参数', '您确定要刪除选中的参数吗?', function(r) {
                        if (r) {
                            $.post(pageJs.url + "?action=del", { data: JSON.stringify(row) }, function(result) {
                                if (result.Success) {
                                    $('#dg').datagrid('reload');    // reload 
                                    BasicJs.showMessage("刪除成功！", true);
                                }
                                else {
                                    if (result.Message) BasicJs.showMessage(result.Message);
                                }
                            }, 'json');
                        }
                    });
                }
                else { BasicJs.showMessage("未选中记录！"); }
            },
            search: function() {
                $('#dg').datagrid('load',
                 {
                     sPARAM_TYPE: $('#sPARAM_TYPE').combobox("getValue"),
                     sPARAM_CODE: $.trim($("#sPARAM_CODE").val()),
                     sPARAM_Name: $.trim($('#sPARAM_NAME').val())
                 }
            );

            },
            dataFilter: function(data) {
                return data;
            }

        }

        $(function() {
            //授权
            var funKey = window.name.substring(3);
            BasicJs.authorize("#add", funKey, "ADD");
            BasicJs.authorize("#edit", funKey, "EDIT");
            BasicJs.authorize("#del", funKey, "DELETE");

            $('#sPARAM_TYPE').combobox({
                url: '../../ashx/System/hParam.ashx?action=getParamType',
                valueField: 'PARAM_TYPE',
                textField: 'PARAM_TYPE',
                method: 'POST',
                onLoadSuccess: function(data) {
                    if (data.length != 0) {
                        $('#sPARAM_TYPE').combobox('setValue', data[0].PARAM_TYPE);
                        pageJs.search();
                    }
                },
                onSelect: function(data) {
                    pageJs.search();
                },
                onChange: function(newValue, oldValue) {

                }
            });
            
            $('#dg').datagrid(
                 {
                     url: "../../ashx/System/hParam.ashx?action=get",
                     loadMsg: "正在加载数据，请稍后...",
                     onLoadSuccess: function(data) {
                         if (data.total > 0) {
                             $('#dg').datagrid("selectRow", 0);
                         }
                     },
                     loadFilter: pageJs.dataFilter
                 }
            );
            pageJs.resize($(window).height(), $(window).width());
            pageJs.url = '../../ashx/System/hParam.ashx';
        });
    </script>
</body>
</html>
